﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OfficeTypeDao
    {
        public bool Delete(SqlConnection conn, int typeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Office_Transhistory] ");
            builder.Append("WHERE ");
            builder.Append("\t[ProductId] IN (SELECT [ProductId] FROM [dbo].[Office_Product] WHERE [TypeId] = @TypeId) ");
            builder.Append("DELETE FROM [dbo].[Office_Product] ");
            builder.Append("WHERE ");
            builder.Append("\t[TypeId] = @TypeId ");
            builder.Append("DELETE FROM [dbo].[Office_Type] ");
            builder.Append("WHERE ");
            builder.Append("\t[TypeId] = @TypeId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@TypeId", SqlDbType.Int) };
            commandParameters[0].Value = typeId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, OfficeTypeData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Office_Type] ");
            builder.Append("( ");
            builder.Append("\t[TypeName]\t ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@TypeName\t ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@TypeName", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = data.TypeName;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[TypeId], ");
            builder.Append("\t[TypeName]\t ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Office_Type] ");
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Office_Type";
            return set.Tables[0];
        }

        public DataTable SelectByTypeId(SqlConnection conn, int typeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[TypeId], ");
            builder.Append("\t[TypeName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Office_Type] ");
            builder.Append("WHERE ");
            builder.Append("\t[TypeId] = @TypeId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@TypeId", SqlDbType.Int) };
            commandParameters[0].Value = typeId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Office_Type";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, OfficeTypeData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Office_Type] ");
            builder.Append("SET ");
            builder.Append("\t[TypeName] = @TypeName\t ");
            builder.Append("WHERE ");
            builder.Append("\t[TypeId] = @TypeId ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@TypeId", SqlDbType.Int);
            commandParameters[0].Value = data.TypeId;
            commandParameters[1] = new SqlParameter("@TypeName", SqlDbType.NVarChar, 50);
            commandParameters[1].Value = data.TypeName;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

